# _*_ coding: utf-8 _*_
from odoo import models, fields, api
from odoo.exceptions import UserError


class MrpBom(models.Model):
    _inherit = "mrp.bom"
    other_boms_count = fields.Integer(string=u"其他BOM", compute="_compute_other_boms_count")

    def _compute_other_boms_count(self):
        for bom in self:
            bom.other_boms_count = self.search_count([
                ("id", "!=", bom.id),
                ('product_id', '=', bom.product_id.id),
            ])

    @api.multi
    def action_view_other_boms(self):
        self.ensure_one()
        action = self.env.ref("mrp.mrp_bom_form_action")
        if not action:
            raise UserError(u"系统里找不到：mrp.mrp_bom_form_action 对应的action")
        other_boms = self.search([("id", "!=", self.id), ('product_id', '=', self.product_id.id)])
        action_vals = action.read()[0]
        if other_boms:
            action_vals["domain"] = [('id', 'in', other_boms.ids)]
        return action_vals
